// Copyright (c) 2025 Yao Yu
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

pub mod elf;

use alloc::{
    sync::Arc,
    vec::Vec,
};
use ostd::{mm::VmSpace, sync::{Mutex, MutexGuard}, task::Task};

pub struct Process {
    pid: u64,
    
    tasks: Mutex<ThreadSet>,

    root_vmar: Mutex<Arc<VmSpace>>,
}

impl Process {
    pub fn new(pid: u64, vm_space: VmSpace) -> Self {
        Process {
            pid,
            tasks: Mutex::new(ThreadSet::new()),
            root_vmar: Mutex::new(Arc::new(vm_space)),
        }
    }
    
    pub fn create_thread(&self, task: Arc<Task>) {
        self.tasks.lock().push(task);
    }

    pub fn get_root_vmar(&self) -> MutexGuard<Arc<VmSpace>> {
        let ret = self.root_vmar.lock();
        ret
    }
}

struct ThreadSet {
    threads: Vec<Arc<Task>>,
}

impl ThreadSet {
    fn new() -> Self {
        ThreadSet {
            threads: Vec::new(),
        }
    }
    fn push(&mut self, task: Arc<Task>) {
        self.threads.push(task);
    }
}